package com.ly.lw.styledo.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.ly.lw.styledo.BaseApp;
import com.ly.lw.styledo.R;
import com.ly.lw.styledo.bean.BrandLogo;
import com.ly.lw.styledo.bean.GridLogo;
import com.ly.lw.styledo.bean.Logo;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by sks on 15-7-7.
 */
public class BrandAllAdapter extends AppBaseAdapter<BrandLogo>{
    private DisplayImageOptions options;

    public BrandAllAdapter(List<BrandLogo> list, Context context) {
        super(list, context);

        DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
        builder.showImageOnLoading(R.mipmap.ic_default_item);
        builder.showImageOnFail(R.mipmap.ic_default_item);
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)//设置下载的图片是否缓存在内存中
                .cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中
                .build();
    }


    @Override
    public View getItemView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_brand_layout, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        final BrandLogo brandLogo = list.get(position);
        String brandImg = list.get(position).getLogo_img();
        brandImg = brandImg.replace("${width}x${height}","186x186");
       // bitmapUtils.display(holder.imageView, list.get(position).getImgUrl());
        ImageLoader.getInstance().displayImage(brandImg, holder.imageView, options);
       // bitmapUtils.display(holder.logoImg, list.get(position).getBrandLogo());
        //ImageLoader.getInstance().displayImage(list.get(position).getBrandLogo(), holder.logoImg, options);
       holder.textLogo.setText(list.get(position).getBrand_nm());
        holder.likeView.setText(list.get(position).getBrand_desc());
        holder.shouCangImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Logo logo = new Logo();
                //GridLogo gridLogo = new GridLogo();
                String brand_desc = brandLogo.getBrand_desc();
                logo.setId(brandLogo.getShop_no());
                logo.setBrand_desc(list.get(position).getBrand_desc());
                logo.setLogo_img(list.get(position).getLogo_img());
                logo.setBrand_nm(list.get(position).getBrand_nm());





                DbUtils dbUtils = BaseApp.getInstance().getDbUtils();

                try {
                    dbUtils.save(logo);
                } catch (DbException e) {
                    e.printStackTrace();
                }

                //Toast.makeText(context,brandLogo.getBrand_desc(), Toast.LENGTH_SHORT).show();
                Toast.makeText(context, "收藏店铺成功", Toast.LENGTH_SHORT).show();
            }
        });

        return convertView;
    }

    private static class ViewHolder {
        ImageView imageView,shouCangImg;
        TextView textLogo;
        TextView likeView;

        public ViewHolder(View itemView) {
            imageView = (ImageView) itemView.findViewById(R.id.item_brand_img);
            textLogo = (TextView) itemView.findViewById(R.id.item_text_logo);
            likeView = (TextView) itemView.findViewById(R.id.brand_desc_id);
            shouCangImg = (ImageView) itemView.findViewById(R.id.shoucang_img);
        }
    }
}
